#include<bits/stdc++.h>
using namespace std;
const int N=301;
int n;
int f[N][N][N];
int a[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    freopen("build.in","r",stdin);
    freopen("build.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        for(int x=1;x<=n;x++)f[i][i][x]=(a[i]==x);
    }
    for(int h=2;h<=n;h++){
        for(int l=1,r=h;r<=n;l++,r++){
            for(int x=1,y=h;y<=n;x++,y++){
                f[l][r][x]=max(f[l][r][x],f[l+1][r][x+1]+(a[l]==x));
                f[l][r][x]=max(f[l][r][x],f[l][r-1][x+1]+(a[r]==x));
                f[l][r][x]=max(f[l][r][x],f[l+1][r][x]+(a[l]==y));
                f[l][r][x]=max(f[l][r][x],f[l][r-1][x]+(a[r]==y));
            }
        }
    }
    cout<<n-f[1][n][1];
    return 0;
}
